package com.pinyougou.manager.controller;

import com.pinyougou.utils.FastDFSClient;
import entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadControlle {
    @Value("${FILE_SERVER_URL}")
    private String FILE_SERVER_URL;

    @RequestMapping("/upload")
    public Result Upload(MultipartFile file) {

        try {
            String originalFilename = file.getOriginalFilename();//获取文件的全名称
            String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);//获取扩展名
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf");
            byte[] bytes = file.getBytes();//获取到组名就是卷名，部署到哪组服务器上的
            String s = fastDFSClient.uploadFile(bytes, substring);
            String url = FILE_SERVER_URL + s;
            return new Result(true, url);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "上传失败");
        }

    }

}
